#j'importe les librairies qui vont me servir plus tard
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.io as pio
#Je definis le nombre de lignes max affichées sur None
pd.set_option("max_rows", None)
pd.set_option("display.precision",7)
#Je charge tous les fichiers csv
data_p8_2tour = pd.read_csv("C:/Users/33643/Desktop/P8 perso/election-presidentielle-2017-resultats-par-bureaux-de-vote-tour-2.csv",sep=';',encoding='utf-8-sig',decimal=",")
#J'affiche une phrase si l'import est reussi
print("Fichiers chargés avec succes!")
Fichiers chargés avec succes!
data_p8_2tour.head()
| Code du département | Département | Code de la circonscription | Circonscription | Code de la commune | Commune | Bureau de vote | Inscrits | Abstentions | % Abs/Ins | ... | Voix | % Voix/Ins | % Voix/Exp | Code Insee | Coordonnées | Nom Bureau Vote | Adresse | Code Postal | Ville | uniq_bdv | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 02 | Aisne | 3 | 3ème circonscription | 308 | Fesmy-le-Sart | 0002 | 119 | 12 | 10.08 | ... | 30 | 25.21 | 31.25 | 02308 | 50.046602,3.698913 | Mairie Annexe de LE SART | NaN | 2450.0 | Fesmy-le-Sart | Mairie Annexe de LE SART - Fesmy-le-Sart |
| 1 | 78 | Yvelines | 10 | 10ème circonscription | 517 | Rambouillet | 0008 | 926 | 146 | 15.77 | ... | 198 | 21.38 | 28.57 | 78517 | 48.653444,1.858915 | Ecole primaire Clairbois | 4 rue du Clos Batant | 78120.0 | Rambouillet | Ecole primaire Clairbois - Rambouillet |
| 2 | 2B | Haute-Corse | 1 | 1ère circonscription | 33 | Bastia | 0024 | 1051 | 434 | 41.29 | ... | 371 | 35.30 | 63.97 | 2B033 | NaN | NaN | NaN | NaN | NaN | NaN |
| 3 | 24 | Dordogne | 2 | 2ème circonscription | 37 | Bergerac | 0010 | 843 | 232 | 27.52 | ... | 374 | 44.37 | 68.75 | 24037 | 44.841728,0.49727000000000005 | École Suzanne Lacorre | NaN | 24100.0 | Bergerac | École Suzanne Lacorre - Bergerac |
| 4 | 64 | Pyrénées-Atlantiques | 1 | 1ère circonscription | 335 | Lescar | 0007 | 779 | 153 | 19.64 | ... | 390 | 50.06 | 70.78 | 64335 | 43.331164,-0.413182 | Complexe sportif Victor Hugo | Rue Maurice Ravel | 64230.0 | Lescar | Complexe sportif Victor Hugo - Lescar |
5 rows × 35 columns
#Je verifie le nombre de lignes et de colonnes
data_p8_2tour.shape
(138510, 35)
#J'affiche le nom des colonnes
data_p8_2tour.columns
Index(['Code du département', 'Département', 'Code de la circonscription',
'Circonscription', 'Code de la commune', 'Commune', 'Bureau de vote',
'Inscrits', 'Abstentions', '% Abs/Ins', 'Votants', '% Vot/Ins',
'Blancs', '% Blancs/Ins', '% Blancs/Vot', 'Nuls', '% Nuls/Ins',
'% Nuls/Vot', 'Exprimés', '% Exp/Ins', '% Exp/Vot', 'N°Panneau', 'Sexe',
'Nom', 'Prénom', 'Voix', '% Voix/Ins', '% Voix/Exp', 'Code Insee',
'Coordonnées', 'Nom Bureau Vote', 'Adresse', 'Code Postal', 'Ville',
'uniq_bdv'],
dtype='object')
#Je supprime les colonnes non utiles
data_p8_2tour = data_p8_2tour.drop(columns =['N°Panneau','Code Insee','Coordonnées','Nom Bureau Vote','Adresse','Ville','uniq_bdv','Code Postal'])
data_p8_2tour.head()
| Code du département | Département | Code de la circonscription | Circonscription | Code de la commune | Commune | Bureau de vote | Inscrits | Abstentions | % Abs/Ins | ... | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Sexe | Nom | Prénom | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 02 | Aisne | 3 | 3ème circonscription | 308 | Fesmy-le-Sart | 0002 | 119 | 12 | 10.08 | ... | 1.87 | 96 | 80.67 | 89.72 | M | MACRON | Emmanuel | 30 | 25.21 | 31.25 |
| 1 | 78 | Yvelines | 10 | 10ème circonscription | 517 | Rambouillet | 0008 | 926 | 146 | 15.77 | ... | 2.69 | 693 | 74.84 | 88.85 | F | LE PEN | Marine | 198 | 21.38 | 28.57 |
| 2 | 2B | Haute-Corse | 1 | 1ère circonscription | 33 | Bastia | 0024 | 1051 | 434 | 41.29 | ... | 1.62 | 580 | 55.19 | 94.00 | F | LE PEN | Marine | 371 | 35.30 | 63.97 |
| 3 | 24 | Dordogne | 2 | 2ème circonscription | 37 | Bergerac | 0010 | 843 | 232 | 27.52 | ... | 2.95 | 544 | 64.53 | 89.03 | M | MACRON | Emmanuel | 374 | 44.37 | 68.75 |
| 4 | 64 | Pyrénées-Atlantiques | 1 | 1ère circonscription | 335 | Lescar | 0007 | 779 | 153 | 19.64 | ... | 3.99 | 551 | 70.73 | 88.02 | M | MACRON | Emmanuel | 390 | 50.06 | 70.78 |
5 rows × 27 columns
print(data_p8_2tour.isnull().any())
print(data_p8_2tour.isnull().sum())
Code du département False Département False Code de la circonscription False Circonscription False Code de la commune False Commune False Bureau de vote False Inscrits False Abstentions False % Abs/Ins False Votants False % Vot/Ins False Blancs False % Blancs/Ins False % Blancs/Vot False Nuls False % Nuls/Ins False % Nuls/Vot False Exprimés False % Exp/Ins False % Exp/Vot False Sexe False Nom False Prénom False Voix False % Voix/Ins False % Voix/Exp False dtype: bool Code du département 0 Département 0 Code de la circonscription 0 Circonscription 0 Code de la commune 0 Commune 0 Bureau de vote 0 Inscrits 0 Abstentions 0 % Abs/Ins 0 Votants 0 % Vot/Ins 0 Blancs 0 % Blancs/Ins 0 % Blancs/Vot 0 Nuls 0 % Nuls/Ins 0 % Nuls/Vot 0 Exprimés 0 % Exp/Ins 0 % Exp/Vot 0 Sexe 0 Nom 0 Prénom 0 Voix 0 % Voix/Ins 0 % Voix/Exp 0 dtype: int64
data_p8_2tour.head()
| Code du département | Département | Code de la circonscription | Circonscription | Code de la commune | Commune | Bureau de vote | Inscrits | Abstentions | % Abs/Ins | ... | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Sexe | Nom | Prénom | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 02 | Aisne | 3 | 3ème circonscription | 308 | Fesmy-le-Sart | 0002 | 119 | 12 | 10.08 | ... | 1.87 | 96 | 80.67 | 89.72 | M | MACRON | Emmanuel | 30 | 25.21 | 31.25 |
| 1 | 78 | Yvelines | 10 | 10ème circonscription | 517 | Rambouillet | 0008 | 926 | 146 | 15.77 | ... | 2.69 | 693 | 74.84 | 88.85 | F | LE PEN | Marine | 198 | 21.38 | 28.57 |
| 2 | 2B | Haute-Corse | 1 | 1ère circonscription | 33 | Bastia | 0024 | 1051 | 434 | 41.29 | ... | 1.62 | 580 | 55.19 | 94.00 | F | LE PEN | Marine | 371 | 35.30 | 63.97 |
| 3 | 24 | Dordogne | 2 | 2ème circonscription | 37 | Bergerac | 0010 | 843 | 232 | 27.52 | ... | 2.95 | 544 | 64.53 | 89.03 | M | MACRON | Emmanuel | 374 | 44.37 | 68.75 |
| 4 | 64 | Pyrénées-Atlantiques | 1 | 1ère circonscription | 335 | Lescar | 0007 | 779 | 153 | 19.64 | ... | 3.99 | 551 | 70.73 | 88.02 | M | MACRON | Emmanuel | 390 | 50.06 | 70.78 |
5 rows × 27 columns
print(data_p8_2tour.duplicated().any())
print(data_p8_2tour.duplicated().sum())
True 26
data_p8_2tour[data_p8_2tour.duplicated()]
| Code du département | Département | Code de la circonscription | Circonscription | Code de la commune | Commune | Bureau de vote | Inscrits | Abstentions | % Abs/Ins | ... | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Sexe | Nom | Prénom | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 32489 | 31 | Haute-Garonne | 5 | 5ème circonscription | 205 | Gagnac-sur-Garonne | 0001 | 1150 | 236 | 20.52 | ... | 2.74 | 820 | 71.30 | 89.72 | F | LE PEN | Marine | 284 | 24.70 | 34.63 |
| 44555 | 50 | Manche | 2 | 2ème circonscription | 484 | Saint-Hilaire-du-Harcouët | 0001 | 1505 | 353 | 23.46 | ... | 7.20 | 1029 | 68.37 | 89.32 | F | LE PEN | Marine | 315 | 20.93 | 30.61 |
| 45616 | 23 | Creuse | 1 | 1ère circonscription | 149 | Parsac-Rimondeix | 0001 | 503 | 121 | 24.06 | ... | 3.66 | 329 | 65.41 | 86.13 | M | MACRON | Emmanuel | 220 | 43.74 | 66.87 |
| 48232 | 65 | Hautes-Pyrénées | 1 | 1ère circonscription | 84 | Bernac-Dessus | 0001 | 217 | 33 | 15.21 | ... | 0.54 | 155 | 71.43 | 84.24 | F | LE PEN | Marine | 52 | 23.96 | 33.55 |
| 54526 | 23 | Creuse | 1 | 1ère circonscription | 149 | Parsac-Rimondeix | 0001 | 503 | 121 | 24.06 | ... | 3.66 | 329 | 65.41 | 86.13 | F | LE PEN | Marine | 109 | 21.67 | 33.13 |
| 54533 | 46 | Lot | 1 | 1ère circonscription | 268 | Saint Géry-Vers | 0001 | 358 | 60 | 16.76 | ... | 3.02 | 250 | 69.83 | 83.89 | M | MACRON | Emmanuel | 181 | 50.56 | 72.40 |
| 55812 | 22 | Côtes-d'Armor | 3 | 3ème circonscription | 183 | Les Moulins | 0001 | 690 | 131 | 18.99 | ... | 3.40 | 479 | 69.42 | 85.69 | M | MACRON | Emmanuel | 301 | 43.62 | 62.84 |
| 84651 | 46 | Lot | 1 | 1ère circonscription | 156 | Bellefont-La Rauze | 0001 | 394 | 89 | 22.59 | ... | 1.64 | 261 | 66.24 | 85.57 | F | LE PEN | Marine | 65 | 16.50 | 24.90 |
| 93943 | 63 | Puy-de-Dôme | 2 | 2ème circonscription | 244 | Chambaron-sur-Morge | 0001 | 424 | 67 | 15.80 | ... | 2.52 | 321 | 75.71 | 89.92 | M | MACRON | Emmanuel | 201 | 47.41 | 62.62 |
| 94410 | 80 | Somme | 5 | 5ème circonscription | 230 | Curchy | 0001 | 232 | 57 | 24.57 | ... | 4.00 | 148 | 63.79 | 84.57 | M | MACRON | Emmanuel | 67 | 28.88 | 45.27 |
| 95792 | 33 | Gironde | 8 | 8ème circonscription | 555 | Marcheprime | 0001 | 1078 | 228 | 21.15 | ... | 7.65 | 743 | 68.92 | 87.41 | F | LE PEN | Marine | 294 | 27.27 | 39.57 |
| 96211 | 63 | Puy-de-Dôme | 2 | 2ème circonscription | 244 | Chambaron-sur-Morge | 0001 | 424 | 67 | 15.80 | ... | 2.52 | 321 | 75.71 | 89.92 | F | LE PEN | Marine | 120 | 28.30 | 37.38 |
| 97344 | 31 | Haute-Garonne | 5 | 5ème circonscription | 205 | Gagnac-sur-Garonne | 0001 | 1150 | 236 | 20.52 | ... | 2.74 | 820 | 71.30 | 89.72 | M | MACRON | Emmanuel | 536 | 46.61 | 65.37 |
| 99899 | 46 | Lot | 1 | 1ère circonscription | 268 | Saint Géry-Vers | 0001 | 358 | 60 | 16.76 | ... | 3.02 | 250 | 69.83 | 83.89 | F | LE PEN | Marine | 69 | 19.27 | 27.60 |
| 101420 | 18 | Cher | 3 | 3ème circonscription | 58 | Châteauneuf-sur-Cher | 0001 | 571 | 169 | 29.60 | ... | 3.73 | 354 | 62.00 | 88.06 | F | LE PEN | Marine | 172 | 30.12 | 48.59 |
| 112000 | 46 | Lot | 1 | 1ère circonscription | 103 | Saint-Paul-Flaugnac | 0001 | 358 | 57 | 15.92 | ... | 2.33 | 263 | 73.46 | 87.38 | M | MACRON | Emmanuel | 189 | 52.79 | 71.86 |
| 115449 | 65 | Hautes-Pyrénées | 1 | 1ère circonscription | 84 | Bernac-Dessus | 0001 | 217 | 33 | 15.21 | ... | 0.54 | 155 | 71.43 | 84.24 | M | MACRON | Emmanuel | 103 | 47.47 | 66.45 |
| 116126 | 50 | Manche | 2 | 2ème circonscription | 484 | Saint-Hilaire-du-Harcouët | 0001 | 1505 | 353 | 23.46 | ... | 7.20 | 1029 | 68.37 | 89.32 | M | MACRON | Emmanuel | 714 | 47.44 | 69.39 |
| 119212 | 18 | Cher | 3 | 3ème circonscription | 58 | Châteauneuf-sur-Cher | 0001 | 571 | 169 | 29.60 | ... | 3.73 | 354 | 62.00 | 88.06 | M | MACRON | Emmanuel | 182 | 31.87 | 51.41 |
| 121721 | 80 | Somme | 5 | 5ème circonscription | 230 | Curchy | 0001 | 232 | 57 | 24.57 | ... | 4.00 | 148 | 63.79 | 84.57 | F | LE PEN | Marine | 81 | 34.91 | 54.73 |
| 122872 | 43 | Haute-Loire | 2 | 2ème circonscription | 187 | Saint-Geneys-près-Saint-Paulien | 0001 | 283 | 38 | 13.43 | ... | 0.41 | 228 | 80.57 | 93.06 | M | MACRON | Emmanuel | 127 | 44.88 | 55.70 |
| 126491 | 46 | Lot | 1 | 1ère circonscription | 156 | Bellefont-La Rauze | 0001 | 394 | 89 | 22.59 | ... | 1.64 | 261 | 66.24 | 85.57 | M | MACRON | Emmanuel | 196 | 49.75 | 75.10 |
| 127906 | 46 | Lot | 1 | 1ère circonscription | 103 | Saint-Paul-Flaugnac | 0001 | 358 | 57 | 15.92 | ... | 2.33 | 263 | 73.46 | 87.38 | F | LE PEN | Marine | 74 | 20.67 | 28.14 |
| 130331 | 22 | Côtes-d'Armor | 3 | 3ème circonscription | 183 | Les Moulins | 0001 | 690 | 131 | 18.99 | ... | 3.40 | 479 | 69.42 | 85.69 | F | LE PEN | Marine | 178 | 25.80 | 37.16 |
| 134252 | 43 | Haute-Loire | 2 | 2ème circonscription | 187 | Saint-Geneys-près-Saint-Paulien | 0001 | 283 | 38 | 13.43 | ... | 0.41 | 228 | 80.57 | 93.06 | F | LE PEN | Marine | 101 | 35.69 | 44.30 |
| 135694 | 33 | Gironde | 8 | 8ème circonscription | 555 | Marcheprime | 0001 | 1078 | 228 | 21.15 | ... | 7.65 | 743 | 68.92 | 87.41 | M | MACRON | Emmanuel | 449 | 41.65 | 60.43 |
26 rows × 27 columns
codes_departement = len(data_p8_2tour['Code du département'].unique())
codes_departement
107
circonscriptions = len(data_p8_2tour['Circonscription'].unique())
circonscriptions
22
communes = len(data_p8_2tour['Commune'].unique())
communes
33363
bureaux = len(data_p8_2tour['Bureau de vote'].unique())
bureaux
1727
nbr_candidats = len(data_p8_2tour['Nom'].unique())
nbr_candidats
2
#Jé crée un dataframe pour le résumé des données
résumé_données = pd.DataFrame({'Nombre': pd.Series([codes_departement, circonscriptions, communes,bureaux,nbr_candidats], index = ['départements', 'circonscriptions', 'communes','bureaux_de_vote','candidats'])})
résumé_données
| Nombre | |
|---|---|
| départements | 107 |
| circonscriptions | 22 |
| communes | 33363 |
| bureaux_de_vote | 1727 |
| candidats | 2 |
data_p8_2tour.head(2)
| Code du département | Département | Code de la circonscription | Circonscription | Code de la commune | Commune | Bureau de vote | Inscrits | Abstentions | % Abs/Ins | ... | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Sexe | Nom | Prénom | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 02 | Aisne | 3 | 3ème circonscription | 308 | Fesmy-le-Sart | 0002 | 119 | 12 | 10.08 | ... | 1.87 | 96 | 80.67 | 89.72 | M | MACRON | Emmanuel | 30 | 25.21 | 31.25 |
| 1 | 78 | Yvelines | 10 | 10ème circonscription | 517 | Rambouillet | 0008 | 926 | 146 | 15.77 | ... | 2.69 | 693 | 74.84 | 88.85 | F | LE PEN | Marine | 198 | 21.38 | 28.57 |
2 rows × 27 columns
participation = data_p8_2tour.groupby('Nom').sum()
participation = participation.reset_index(drop=False)
participation.head(2)
| Nom | Code de la circonscription | Code de la commune | Inscrits | Abstentions | % Abs/Ins | Votants | % Vot/Ins | Blancs | % Blancs/Ins | % Blancs/Vot | Nuls | % Nuls/Ins | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | LE PEN | 288976 | 16198557 | 47576456 | 12103005 | 1614081.15 | 35473451 | 5311320.97 | 3021983 | 482057.15 | 622921.69 | 1064485 | 174367.42 | 227718.33 | 31386983 | 4654898.02 | 6.0746643e+06 | 10640389 | 1739267.06 | 2577465.57 |
| 1 | MACRON | 288976 | 16198557 | 47576456 | 12103005 | 1614081.15 | 35473451 | 5311320.97 | 3021983 | 482057.15 | 622921.69 | 1064485 | 174367.42 | 227718.33 | 31386983 | 4654898.02 | 6.0746643e+06 | 20746594 | 2915629.02 | 4344036.87 |
inscrits_sum = participation.loc[participation['Nom'] == 'MACRON','Inscrits']
inscrits_sum = inscrits_sum.to_string(index = False)
inscrits_sum
'47576456'
absentions_sum = participation.loc[participation['Nom'] == 'MACRON','Abstentions']
absentions_sum = absentions_sum.to_string(index = False)
absentions_sum
'12103005'
votans_sum = participation.loc[participation['Nom'] == 'MACRON','Votants']
votans_sum = votans_sum.to_string(index = False)
votans_sum
'35473451'
blancs_sum = participation.loc[participation['Nom'] == 'MACRON','Blancs']
blancs_sum = blancs_sum.to_string(index = False)
blancs_sum
'3021983'
nuls_sum = participation.loc[participation['Nom'] == 'MACRON','Nuls']
nuls_sum = nuls_sum.to_string(index = False)
nuls_sum
'1064485'
exprimés_sum = participation.loc[participation['Nom'] == 'MACRON','Exprimés']
exprimés_sum = exprimés_sum.to_string(index = False)
exprimés_sum
'31386983'
participation_ratio = data_p8_2tour.groupby('Nom').mean()
participation_ratio = participation_ratio.reset_index(drop=False)
participation_ratio.head(2)
| Nom | Code de la circonscription | Code de la commune | Inscrits | Abstentions | % Abs/Ins | Votants | % Vot/Ins | Blancs | % Blancs/Ins | % Blancs/Vot | Nuls | % Nuls/Ins | % Nuls/Vot | Exprimés | % Exp/Ins | % Exp/Vot | Voix | % Voix/Ins | % Voix/Exp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | LE PEN | 4.1726374 | 233.8972926 | 686.9750343 | 174.7600173 | 23.3063483 | 512.215017 | 76.6922384 | 43.6355931 | 6.9606115 | 8.9946096 | 15.3705148 | 2.5177593 | 3.2881139 | 453.2089091 | 67.213891 | 87.7144514 | 153.6407335 | 25.1139565 | 37.2170323 |
| 1 | MACRON | 4.1726374 | 233.8972926 | 686.9750343 | 174.7600173 | 23.3063483 | 512.215017 | 76.6922384 | 43.6355931 | 6.9606115 | 8.9946096 | 15.3705148 | 2.5177593 | 3.2881139 | 453.2089091 | 67.213891 | 87.7144514 | 299.5681756 | 42.0999064 | 62.7252454 |
abs_ratio = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Abs/Ins']
abs_ratio = (round(abs_ratio, 2))
abs_ratio = abs_ratio.to_string(index = False)
abs_ratio
'23.31'
vot_ratio = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Vot/Ins']
vot_ratio = (round(vot_ratio, 2))
vot_ratio = vot_ratio.to_string(index = False)
vot_ratio
'76.69'
blancs_ratio = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Blancs/Ins']
blancs_ratio = (round(blancs_ratio, 2))
blancs_ratio = blancs_ratio.to_string(index = False)
blancs_ratio
'6.96'
blancs_ratio2 = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Blancs/Vot']
blancs_ratio2 = (round(blancs_ratio2, 2))
blancs_ratio2 = blancs_ratio2.to_string(index = False)
blancs_ratio2
'8.99'
nuls_ratio = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Nuls/Ins']
nuls_ratio = (round(nuls_ratio, 2))
nuls_ratio = nuls_ratio.to_string(index = False)
nuls_ratio
'2.52'
nuls_ratio2 = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Nuls/Vot']
nuls_ratio2 = (round(nuls_ratio2, 2))
nuls_ratio2 = nuls_ratio2.to_string(index = False)
nuls_ratio2
'3.29'
exp_ratio = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Exp/Ins']
exp_ratio = (round(exp_ratio, 2))
exp_ratio = exp_ratio.to_string(index = False)
exp_ratio
'67.21'
exp_ratio2 = participation_ratio.loc[participation_ratio['Nom'] == 'MACRON','% Exp/Vot']
exp_ratio2 = (round(exp_ratio2, 2))
exp_ratio2 = exp_ratio2.to_string(index = False)
exp_ratio2
'87.71'
#Jé crée un dataframe pour le nombre d'inscrits, abstentions, votants, blancs, nuls, exprimés
participation = pd.DataFrame({'Nombre': pd.Series([inscrits_sum, absentions_sum, votans_sum,blancs_sum,nuls_sum,exprimés_sum], index = ['Inscrits', 'Abstentions', 'Votans','Blancs','Nuls','Exprimés']),
'% des inscrits': pd.Series(['',abs_ratio, vot_ratio,blancs_ratio,nuls_ratio,exp_ratio], index = ['Inscrits', 'Abstentions', 'Votans','Blancs','Nuls','Exprimés']),
'% des votants': pd.Series(['','',100,blancs_ratio2,nuls_ratio2,exp_ratio2], index = ['Inscrits', 'Abstentions', 'Votans','Blancs','Nuls','Exprimés'])})
participation
| Nombre | % des inscrits | % des votants | |
|---|---|---|---|
| Inscrits | 47576456 | ||
| Abstentions | 12103005 | 23.31 | |
| Votans | 35473451 | 76.69 | 100 |
| Blancs | 3021983 | 6.96 | 8.99 |
| Nuls | 1064485 | 2.52 | 3.29 |
| Exprimés | 31386983 | 67.21 | 87.71 |
# bar chart
value = [66920000,47576456,35473451,31386983,12103005,3021983,1064485]
labels = ['Population 2017','Inscrits','Votants','Exprimés','Abstentions','Blancs','Nuls']
#participation_viz = participation_viz.sort_values('Nombre',ascending=[False])
pio.templates.default = "plotly_white"
fig = px.bar(x=labels, y=value,width=800, height=600,text=value, labels={ "x": " ","y": "nombre de voix"})
fig.update_traces(textposition = 'outside',texttemplate='%{text:,}', textfont_size=12)
#fig.update_traces(marker_color='dimgray')
fig.update_layout(title_text='<b>Participation au 2e Tour', title_y=0.97,title_x=0.55,title_font = dict(size=25, color='dimgray'))
fig.update_layout(separators = '.,',margin=dict(t=50, b=0, l=0, r=0))
#fig.update_layout(legend=dict(yanchor="auto", y=0.5, xanchor="auto", x=1.3),legend_title_text='')
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
#fig.update_yaxes(visible=False)
fig.show()
# bar chart
value = [76.69,67.21,23.31,6.96,2.52]
labels = ['% Vot/ins','% Exp/ins','% abs/ins','% Blancs/ins','% Nuls/ins']
#participation_viz = participation_viz.sort_values('Nombre',ascending=[False])
pio.templates.default = "plotly_white"
fig = px.bar(x=labels, y=value,width=800, height=600,text=value, labels={ "x": " ","y": "% des voix"})
fig.update_traces(textposition = 'outside',texttemplate='%{text:,}', textfont_size=12)
#fig.update_traces(marker_color='dimgray')
fig.update_layout(title_text='<b>Ratio inscrits au 2e Tour', title_y=0.97,title_x=0.55,title_font = dict(size=25, color='dimgray'))
fig.update_layout(separators = '.,',margin=dict(t=50, b=0, l=0, r=0))
#fig.update_layout(legend=dict(yanchor="auto", y=0.5, xanchor="auto", x=1.3),legend_title_text='')
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
#fig.update_yaxes(visible=False)
fig.show()
# bar chart
value = [87.71,8.99,3.29]
labels = ['% Exp/vot','% Blancs/vot','% nuls/vot']
#participation_viz = participation_viz.sort_values('Nombre',ascending=[False])
pio.templates.default = "plotly_white"
fig = px.bar(x=labels, y=value,width=800, height=600,text=value, labels={ "x": " ","y": "% des voix"})
fig.update_traces(textposition = 'outside',texttemplate='%{text:,}', textfont_size=12)
#fig.update_traces(marker_color='dimgray')
fig.update_layout(title_text='<b>Ratio votants au 2e Tour', title_y=0.97,title_x=0.55,title_font = dict(size=25, color='dimgray'))
fig.update_layout(separators = '.,',margin=dict(t=50, b=0, l=0, r=0))
#fig.update_layout(legend=dict(yanchor="auto", y=0.5, xanchor="auto", x=1.3),legend_title_text='')
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
#fig.update_yaxes(visible=False)
fig.show()
classement_dep_abs = data_p8_2tour.groupby('Département').mean().sort_values('% Abs/Ins',ascending=[False])
classement_dep_abs = classement_dep_abs.reset_index(drop=False)
classement_dep_abs = classement_dep_abs.drop(columns =['Code de la circonscription','Code de la commune','Votants','% Vot/Ins','Blancs','% Blancs/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_abs_top30 = classement_dep_abs.head(30)
classement_dep_abs.head(5)
| Département | Inscrits | Abstentions | % Abs/Ins | |
|---|---|---|---|---|
| 0 | Saint-Martin/Saint-Barthélemy | 1060.2083333 | 685.5000000 | 64.5137500 |
| 1 | Guyane | 805.2260870 | 468.8347826 | 58.7340870 |
| 2 | Mayotte | 468.6477273 | 254.3125000 | 53.2905682 |
| 3 | Français établis hors de France | 5673.6771300 | 3072.9865471 | 52.7011659 |
| 4 | Nouvelle-Calédonie | 701.3629630 | 331.0000000 | 52.4299630 |
abs_top_1 = classement_dep_abs.loc[classement_dep_abs['Département'] == "Saint-Martin/Saint-Barthélemy"]
abs_top_1 = abs_top_1["% Abs/Ins"]
abs_top_1 = abs_top_1.to_string(index=False)
abs_top_1 = "{0:.2f}".format(float(abs_top_1))
abs_top_1
'64.51'
abs_top_2 = classement_dep_abs.loc[classement_dep_abs['Département'] == "Guyane"]
abs_top_2 = abs_top_2["% Abs/Ins"]
abs_top_2 = abs_top_2.to_string(index=False)
abs_top_2 = "{0:.2f}".format(float(abs_top_2))
abs_top_2
'58.73'
abs_top_3 = classement_dep_abs.loc[classement_dep_abs['Département'] == "Mayotte"]
abs_top_3 = abs_top_3["% Abs/Ins"]
abs_top_3 = abs_top_3.to_string(index=False)
abs_top_3 = "{0:.2f}".format(float(abs_top_3))
abs_top_3
'53.29'
# scatter plot
pio.templates.default = "plotly_white"
#text=classement_dep_blanc3['% Blancs/Vot']
#labels={ "Département": " "}
fig = px.scatter(classement_dep_abs_top30, x="Département", y="% Abs/Ins",labels={ "Département": " "})
fig.update_layout(title_text="<b>Ratio des abstentions/département au 2e Tour", title_y=0.9,title_x=0.5,title_font = dict(size=25, color='dimgray'))
fig.update_layout(margin=dict(t=100, b=0, l=0, r=0))
#fig.update_yaxes(visible=False)
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.add_annotation(x= "Saint-Martin/Saint-Barthélemy", y=abs_top_1, text=abs_top_1,xanchor='auto')
fig.add_annotation(x= "Guyane", y=abs_top_2, text=abs_top_2,xanchor='left')
fig.add_annotation(x= "Mayotte", y=abs_top_3, text=abs_top_3,xanchor='left')
fig.update_layout(margin_pad=10)
#fig.update_yaxes(title=None)
fig.show()
classement_dep_abs2 = data_p8_2tour.groupby('Département').mean().sort_values('% Abs/Ins')
classement_dep_abs2 = classement_dep_abs2.reset_index(drop=False)
classement_dep_abs2 = classement_dep_abs2.drop(columns =['Code de la circonscription','Code de la commune','Votants','% Vot/Ins','Blancs','% Blancs/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_abs2.head(5)
| Département | Inscrits | Abstentions | % Abs/Ins | |
|---|---|---|---|---|
| 0 | Gers | 284.7740668 | 54.2927308 | 17.3791749 |
| 1 | Lot | 347.7848101 | 69.6278481 | 18.5952658 |
| 2 | Jura | 326.4425386 | 70.4322470 | 18.9302401 |
| 3 | Côtes-d'Armor | 696.9923664 | 134.0473282 | 18.9900000 |
| 4 | Corrèze | 482.4088542 | 99.3750000 | 18.9965625 |
classement_dep_vot = data_p8_2tour.groupby('Département').mean().sort_values('% Vot/Ins',ascending=[False])
classement_dep_vot = classement_dep_vot.reset_index(drop=False)
classement_dep_vot = classement_dep_vot.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Blancs','% Blancs/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_vot.head(5)
| Département | Inscrits | Votants | % Vot/Ins | |
|---|---|---|---|---|
| 0 | Gers | 284.7740668 | 230.4813360 | 82.6209234 |
| 1 | Lot | 347.7848101 | 278.1569620 | 81.4048608 |
| 2 | Jura | 326.4425386 | 256.0102916 | 81.0697770 |
| 3 | Côtes-d'Armor | 696.9923664 | 562.9450382 | 81.0100458 |
| 4 | Corrèze | 482.4088542 | 383.0338542 | 81.0034635 |
classement_dep_vot2 = data_p8_2tour.groupby('Département').mean().sort_values('% Vot/Ins')
classement_dep_vot2 = classement_dep_vot2.reset_index(drop=False)
classement_dep_vot2 = classement_dep_vot2.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Blancs','% Blancs/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_vot2_top30 = classement_dep_vot2.head(30)
classement_dep_vot2.head(5)
| Département | Inscrits | Votants | % Vot/Ins | |
|---|---|---|---|---|
| 0 | Saint-Martin/Saint-Barthélemy | 1060.2083333 | 374.7083333 | 35.4862500 |
| 1 | Guyane | 805.2260870 | 336.3913043 | 41.2659130 |
| 2 | Mayotte | 468.6477273 | 214.3352273 | 46.7094318 |
| 3 | Français établis hors de France | 5673.6771300 | 2600.6905830 | 46.8504036 |
| 4 | Nouvelle-Calédonie | 701.3629630 | 370.3629630 | 47.5701111 |
vot_top_1 = classement_dep_vot2.loc[classement_dep_vot2['Département'] == "Saint-Martin/Saint-Barthélemy"]
vot_top_1 = vot_top_1["% Vot/Ins"]
vot_top_1 = vot_top_1.to_string(index=False)
vot_top_1 = "{0:.2f}".format(float(vot_top_1))
vot_top_1
'35.49'
vot_top_2 = classement_dep_vot2.loc[classement_dep_vot2['Département'] == "Guyane"]
vot_top_2 = vot_top_2["% Vot/Ins"]
vot_top_2 = vot_top_2.to_string(index=False)
vot_top_2 = "{0:.2f}".format(float(vot_top_2))
vot_top_2
'41.27'
vot_top_3 = classement_dep_vot2.loc[classement_dep_vot2['Département'] == "Mayotte"]
vot_top_3 = vot_top_3["% Vot/Ins"]
vot_top_3 = vot_top_3.to_string(index=False)
vot_top_3 = "{0:.2f}".format(float(vot_top_3))
vot_top_3
'46.71'
# scatter plot
pio.templates.default = "plotly_white"
#text=classement_dep_blanc3['% Blancs/Vot']
#labels={ "Département": " "}
fig = px.scatter(classement_dep_vot2_top30, x="Département", y="% Vot/Ins",labels={ "Département": " "})
fig.update_layout(title_text="<b>Ratio des votants/département au 2e Tour", title_y=0.9,title_x=0.5,title_font = dict(size=25, color='dimgray'))
fig.update_layout(margin=dict(t=100, b=0, l=0, r=0))
#fig.update_yaxes(visible=False)
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.add_annotation(x= "Saint-Martin/Saint-Barthélemy", y=vot_top_1, text=vot_top_1,xanchor='right')
fig.add_annotation(x= "Guyane", y=vot_top_2, text=vot_top_2,xanchor='auto')
fig.add_annotation(x= "Mayotte", y=vot_top_3, text=vot_top_3,xanchor='auto')
fig.update_layout(margin_pad=10)
fig.show()
classement_dep_blanc = data_p8_2tour.groupby('Département').mean().sort_values('% Blancs/Ins',ascending=[False])
classement_dep_blanc = classement_dep_blanc.reset_index(drop=False)
classement_dep_blanc = classement_dep_blanc.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_blanc.head(5)
| Département | Inscrits | Blancs | % Blancs/Ins | |
|---|---|---|---|---|
| 0 | Ariège | 306.5430809 | 26.4569191 | 9.8334204 |
| 1 | Lozère | 298.9748744 | 25.1658291 | 9.3273869 |
| 2 | Lot | 347.7848101 | 29.2734177 | 9.1437215 |
| 3 | Jura | 326.4425386 | 26.3481990 | 8.9664666 |
| 4 | Hautes-Pyrénées | 313.7477954 | 25.0440917 | 8.9278131 |
classement_dep_blanc2 = data_p8_2tour.groupby('Département').mean().sort_values('% Blancs/Ins')
classement_dep_blanc2 = classement_dep_blanc2.reset_index(drop=False)
classement_dep_blanc2 = classement_dep_blanc2.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_blanc2.head(5)
| Département | Inscrits | Blancs | % Blancs/Ins | |
|---|---|---|---|---|
| 0 | Wallis et Futuna | 8464.0000000 | 76.0000000 | 0.9000000 |
| 1 | Polynésie française | 864.2923729 | 13.8686441 | 1.3827966 |
| 2 | Mayotte | 468.6477273 | 8.1931818 | 1.7861364 |
| 3 | Français établis hors de France | 5673.6771300 | 87.5067265 | 2.3317040 |
| 4 | Saint-Martin/Saint-Barthélemy | 1060.2083333 | 26.5833333 | 2.5008333 |
classement_dep_blanc3 = data_p8_2tour.groupby('Département').mean().sort_values('% Blancs/Vot',ascending=[False])
classement_dep_blanc3 = classement_dep_blanc3.reset_index(drop=False)
classement_dep_blanc3 = classement_dep_blanc3.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Inscrits','% Vot/Ins','% Blancs/Ins','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_blanc3_top30 = classement_dep_blanc3.head(30)
classement_dep_blanc3.head(5)
| Département | Votants | Blancs | % Blancs/Vot | |
|---|---|---|---|---|
| 0 | Ariège | 236.8355091 | 26.4569191 | 12.5036031 |
| 1 | Lozère | 239.2562814 | 25.1658291 | 11.5488442 |
| 2 | Creuse | 252.6971831 | 27.6232394 | 11.2442254 |
| 3 | Lot | 278.1569620 | 29.2734177 | 11.2325316 |
| 4 | Hautes-Pyrénées | 241.9753086 | 25.0440917 | 11.1784127 |
top_1 = classement_dep_blanc3.loc[classement_dep_blanc3['Département'] == "Ariège"]
top_1 = top_1["% Blancs/Vot"]
top_1 = top_1.to_string(index=False)
top_1 = "{0:.2f}".format(float(top_1))
top_1
'12.50'
top_2 = classement_dep_blanc3.loc[classement_dep_blanc3['Département'] == "Lozère"]
top_2 = top_2["% Blancs/Vot"]
top_2 = top_2.to_string(index=False)
top_2 = "{0:.2f}".format(float(top_2))
top_2
'11.55'
top_3 = classement_dep_blanc3.loc[classement_dep_blanc3['Département'] == "Creuse"]
top_3 = top_3["% Blancs/Vot"]
top_3 = top_3.to_string(index=False)
top_3 = "{0:.2f}".format(float(top_3))
top_3
'11.24'
# scatter plot
pio.templates.default = "plotly_white"
#text=classement_dep_blanc3['% Blancs/Vot']
#labels={ "Département": " "}
fig = px.scatter(classement_dep_blanc3_top30, x="Département", y="% Blancs/Vot",labels={ "Département": " "})
fig.update_layout(title_text="<b>Ratio de votes blancs/votants par département au 2e Tour", title_y=0.9,title_x=0.5,title_font = dict(size=25, color='dimgray'))
fig.update_layout(margin=dict(t=100, b=0, l=0, r=0))
#fig.update_yaxes(visible=False)
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.add_annotation(x= "Ariège", y=top_1, text=top_1)
fig.add_annotation(x= "Lozère", y=top_2, text=top_2)
fig.add_annotation(x= "Creuse", y=top_3, text=top_3)
fig.show()
classement_dep_blanc4 = data_p8_2tour.groupby('Département').mean().sort_values('% Blancs/Vot')
classement_dep_blanc4 = classement_dep_blanc4.reset_index(drop=False)
classement_dep_blanc4 = classement_dep_blanc4.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','% Abs/Ins','Inscrits','% Vot/Ins','% Blancs/Ins','Nuls','% Nuls/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_blanc4.head(5)
| Département | Votants | Blancs | % Blancs/Vot | |
|---|---|---|---|---|
| 0 | Wallis et Futuna | 6095.0000000 | 76.0000000 | 1.2500000 |
| 1 | Polynésie française | 405.2669492 | 13.8686441 | 2.9441949 |
| 2 | Mayotte | 214.3352273 | 8.1931818 | 3.8318182 |
| 3 | Nouvelle-Calédonie | 370.3629630 | 24.2037037 | 4.7177778 |
| 4 | Français établis hors de France | 2600.6905830 | 87.5067265 | 4.8361883 |
classement_dep_nul = data_p8_2tour.groupby('Département').mean().sort_values('% Nuls/Ins',ascending=[False])
classement_dep_nul = classement_dep_nul.reset_index(drop=False)
classement_dep_nul = classement_dep_nul.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_nul.head(5)
| Département | Inscrits | Nuls | % Nuls/Ins | |
|---|---|---|---|---|
| 0 | Corrèze | 482.4088542 | 20.7447917 | 4.7416927 |
| 1 | Haute-Saône | 289.9466019 | 11.8964401 | 4.4034951 |
| 2 | Creuse | 329.6654930 | 13.8204225 | 4.2960915 |
| 3 | Haute-Vienne | 713.9651475 | 29.1635389 | 4.1384182 |
| 4 | Ariège | 306.5430809 | 11.7963446 | 4.1229504 |
classement_dep_nul2 = data_p8_2tour.groupby('Département').mean().sort_values('% Nuls/Ins')
classement_dep_nul2 = classement_dep_nul2.reset_index(drop=False)
classement_dep_nul2 = classement_dep_nul2.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_nul2.head(5)
| Département | Inscrits | Nuls | % Nuls/Ins | |
|---|---|---|---|---|
| 0 | Français établis hors de France | 5673.6771300 | 20.9910314 | 0.5357399 |
| 1 | Wallis et Futuna | 8464.0000000 | 61.0000000 | 0.7200000 |
| 2 | Saint-Martin/Saint-Barthélemy | 1060.2083333 | 9.7083333 | 0.9000000 |
| 3 | Hauts-de-Seine | 969.9428008 | 9.1932939 | 0.9606213 |
| 4 | Nouvelle-Calédonie | 701.3629630 | 8.6814815 | 1.0584074 |
classement_dep_nul3 = data_p8_2tour.groupby('Département').mean().sort_values('% Nuls/Vot',ascending=[False])
classement_dep_nul3 = classement_dep_nul3.reset_index(drop=False)
classement_dep_nul3 = classement_dep_nul3.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Inscrits','% Vot/Ins','% Blancs/Vot','% Nuls/Ins','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_nul3_top30 = classement_dep_nul3.head(30)
classement_dep_nul3.head(5)
| Département | Votants | Nuls | % Nuls/Vot | |
|---|---|---|---|---|
| 0 | Mayotte | 214.3352273 | 15.7215909 | 7.3939205 |
| 1 | Guadeloupe | 375.8695652 | 27.4009662 | 7.3604348 |
| 2 | La Réunion | 443.9845475 | 26.3432671 | 5.9659713 |
| 3 | Corrèze | 383.0338542 | 20.7447917 | 5.8271875 |
| 4 | Martinique | 474.5714286 | 27.1003040 | 5.7613982 |
nul_top_1 = classement_dep_nul3.loc[classement_dep_nul3['Département'] == "Mayotte"]
nul_top_1 = nul_top_1["% Nuls/Vot"]
nul_top_1 = nul_top_1.to_string(index=False)
nul_top_1 = "{0:.2f}".format(float(nul_top_1))
nul_top_1
'7.39'
nul_top_2 = classement_dep_nul3.loc[classement_dep_nul3['Département'] == "Guadeloupe"]
nul_top_2 = nul_top_2["% Nuls/Vot"]
nul_top_2 = nul_top_2.to_string(index=False)
nul_top_2 = "{0:.2f}".format(float(nul_top_2))
nul_top_2
'7.36'
nul_top_3 = classement_dep_nul3.loc[classement_dep_nul3['Département'] == "La Réunion"]
nul_top_3 = nul_top_3["% Nuls/Vot"]
nul_top_3 = nul_top_3.to_string(index=False)
nul_top_3 = "{0:.2f}".format(float(nul_top_3))
nul_top_3
'5.97'
# scatter plot
pio.templates.default = "plotly_white"
#text=classement_dep_blanc3['% Blancs/Vot']
#labels={ "Département": " "}
fig = px.scatter(classement_dep_nul3_top30, x="Département", y="% Nuls/Vot",labels={ "Département": " "})
fig.update_layout(title_text="<b>Ratio de votes nuls/votants par département au 2e Tour", title_y=0.9,title_x=0.5,title_font = dict(size=25, color='dimgray'))
fig.update_layout(margin=dict(t=100, b=0, l=0, r=0))
#fig.update_yaxes(visible=False)
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.add_annotation(x= "Mayotte", y=nul_top_1, text=nul_top_1)
fig.add_annotation(x= "Guadeloupe", y=nul_top_2, text=nul_top_2)
fig.add_annotation(x= "La Réunion", y=nul_top_3, text=nul_top_3)
fig.show()
classement_dep_nul4 = data_p8_2tour.groupby('Département').mean().sort_values('% Nuls/Vot')
classement_dep_nul4 = classement_dep_nul4.reset_index(drop=False)
classement_dep_nul4 = classement_dep_nul4.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Inscrits','% Vot/Ins','% Blancs/Vot','% Nuls/Ins','Exprimés','% Exp/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_nul4.head(5)
| Département | Votants | Nuls | % Nuls/Vot | |
|---|---|---|---|---|
| 0 | Wallis et Futuna | 6095.0000000 | 61.0000000 | 1.0000000 |
| 1 | Français établis hors de France | 2600.6905830 | 20.9910314 | 1.1352466 |
| 2 | Hauts-de-Seine | 746.2287968 | 9.1932939 | 1.2652071 |
| 3 | Paris | 1140.2220982 | 18.6875000 | 1.6477902 |
| 4 | Nouvelle-Calédonie | 370.3629630 | 8.6814815 | 2.1156667 |
classement_dep_exp = data_p8_2tour.groupby('Département').mean().sort_values('% Exp/Ins',ascending=[False])
classement_dep_exp = classement_dep_exp.reset_index(drop=False)
classement_dep_exp = classement_dep_exp.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Nuls','% Nuls/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_exp.head(5)
| Département | Inscrits | Exprimés | % Exp/Ins | |
|---|---|---|---|---|
| 0 | Paris | 1452.6082589 | 1056.9497768 | 72.7651339 |
| 1 | Somme | 412.4889113 | 285.2207661 | 71.4218448 |
| 2 | Ille-et-Vilaine | 840.1093211 | 597.5707710 | 71.4002992 |
| 3 | Vendée | 854.5265781 | 602.9053156 | 70.7357973 |
| 4 | Côtes-d'Armor | 696.9923664 | 493.1083969 | 70.6479084 |
classement_dep_exp2 = data_p8_2tour.groupby('Département').mean().sort_values('% Exp/Ins')
classement_dep_exp2 = classement_dep_exp2.reset_index(drop=False)
classement_dep_exp2 = classement_dep_exp2.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Nuls','% Nuls/Ins','% Exp/Vot','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_exp2_top30 = classement_dep_exp2.head(30)
classement_dep_exp2.head(5)
| Département | Inscrits | Exprimés | % Exp/Ins | |
|---|---|---|---|---|
| 0 | Saint-Martin/Saint-Barthélemy | 1060.2083333 | 338.4166667 | 32.0862500 |
| 1 | Guyane | 805.2260870 | 291.7043478 | 35.9451304 |
| 2 | Mayotte | 468.6477273 | 190.4204545 | 41.4913636 |
| 3 | Guadeloupe | 763.4347826 | 323.5386473 | 42.3642754 |
| 4 | Martinique | 944.2948328 | 408.8206687 | 43.3445289 |
exp_top_1 = classement_dep_exp2.loc[classement_dep_exp2['Département'] == "Saint-Martin/Saint-Barthélemy"]
exp_top_1 = exp_top_1["% Exp/Ins"]
exp_top_1 = exp_top_1.to_string(index=False)
exp_top_1 = "{0:.2f}".format(float(exp_top_1))
exp_top_1
'32.09'
exp_top_2 = classement_dep_exp2.loc[classement_dep_exp2['Département'] == "Guyane"]
exp_top_2 = exp_top_2["% Exp/Ins"]
exp_top_2 = exp_top_2.to_string(index=False)
exp_top_2 = "{0:.2f}".format(float(exp_top_2))
exp_top_2
'35.95'
exp_top_3 = classement_dep_exp2.loc[classement_dep_exp2['Département'] == "Mayotte"]
exp_top_3 = exp_top_3["% Exp/Ins"]
exp_top_3 = exp_top_3.to_string(index=False)
exp_top_3 = "{0:.2f}".format(float(exp_top_3))
exp_top_3
'41.49'
# scatter plot
pio.templates.default = "plotly_white"
#text=classement_dep_blanc3['% Blancs/Vot']
#labels={ "Département": " "}
fig = px.scatter(classement_dep_exp2_top30, x="Département", y="% Exp/Ins",labels={ "Département": " "})
fig.update_layout(title_text="<b>Ratio des exprimés/département au 2e Tour", title_y=0.9,title_x=0.5,title_font = dict(size=25, color='dimgray'))
fig.update_layout(margin=dict(t=100, b=0, l=0, r=0))
#fig.update_yaxes(visible=False)
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.add_annotation(x= "Saint-Martin/Saint-Barthélemy", y=exp_top_1, text=exp_top_1)
fig.add_annotation(x= "Guyane", y=exp_top_2, text=exp_top_2)
fig.add_annotation(x= "Mayotte", y=exp_top_3, text=exp_top_3)
fig.show()
classement_dep_exp3 = data_p8_2tour.groupby('Département').mean().sort_values('% Exp/Vot',ascending=[False])
classement_dep_exp3 = classement_dep_exp3.reset_index(drop=False)
classement_dep_exp3 = classement_dep_exp3.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Nuls','% Nuls/Ins','% Exp/Ins','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_exp3.head(5)
| Département | Inscrits | Exprimés | % Exp/Vot | |
|---|---|---|---|---|
| 0 | Wallis et Futuna | 8464.0000000 | 5958.0000000 | 97.7500000 |
| 1 | Polynésie française | 864.2923729 | 380.0720339 | 94.3482203 |
| 2 | Nouvelle-Calédonie | 701.3629630 | 337.4777778 | 93.1662963 |
| 3 | Français établis hors de France | 5673.6771300 | 2492.1928251 | 93.1316143 |
| 4 | Paris | 1452.6082589 | 1056.9497768 | 92.6607701 |
classement_dep_exp4 = data_p8_2tour.groupby('Département').mean().sort_values('% Exp/Vot')
classement_dep_exp4 = classement_dep_exp4.reset_index(drop=False)
classement_dep_exp4 = classement_dep_exp4.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Nuls','% Nuls/Ins','% Exp/Ins','Voix','% Voix/Ins','% Voix/Exp'])
classement_dep_exp4.head(5)
| Département | Inscrits | Exprimés | % Exp/Vot | |
|---|---|---|---|---|
| 0 | Ariège | 306.5430809 | 198.5822454 | 82.2198695 |
| 1 | Creuse | 329.6654930 | 211.2535211 | 83.2272535 |
| 2 | Corrèze | 482.4088542 | 322.8750000 | 83.2571875 |
| 3 | Lozère | 298.9748744 | 202.3567839 | 83.4767337 |
| 4 | Saint-Pierre-et-Miquelon | 1240.7500000 | 579.5000000 | 83.9450000 |
participation2 = data_p8_2tour.groupby('Nom').sum().sort_values('Voix',ascending=[False])
participation2 = participation2.reset_index(drop=False)
participation2 = participation2.drop(columns =['Code de la circonscription','Code de la commune','Abstentions','Blancs','% Blancs/Ins','% Abs/Ins','Votants','% Vot/Ins','% Blancs/Vot','% Nuls/Vot','Nuls','% Nuls/Ins','% Exp/Vot','% Exp/Ins','% Voix/Ins','% Voix/Exp'])
participation2.head(2)
| Nom | Inscrits | Exprimés | Voix | |
|---|---|---|---|---|
| 0 | MACRON | 47576456 | 31386983 | 20746594 |
| 1 | LE PEN | 47576456 | 31386983 | 10640389 |
participation2['ratio_inscrits'] = round((participation2['Voix'] / participation2['Inscrits'])*100,2)
participation2['ratio_exprimés'] = round((participation2['Voix'] / participation2['Exprimés'])*100,2)
participation2 = participation2.drop(columns =['Inscrits','Exprimés'])
participation2.head(2)
| Nom | Voix | ratio_inscrits | ratio_exprimés | |
|---|---|---|---|---|
| 0 | MACRON | 20746594 | 43.61 | 66.1 |
| 1 | LE PEN | 10640389 | 22.36 | 33.9 |
# pie chart
fig = px.pie(participation2, values='ratio_exprimés', names='Nom',width=700, height=600, hole=0.5, color = 'Nom',color_discrete_map={
'MACRON':'deeppink',
'LE PEN':'darkblue'})
fig.update_traces(textposition = 'inside',textfont_size=12, textinfo='percent+label',pull=[0.2,0,0,0,0,0,0,0,0,0,0],marker = dict(line=dict(color='white', width=3)))
fig.update_layout(title_text='<b>Ratio des votes/candidat au 2e Tour', title_y=0.97,title_x=0.55,title_font = dict(size=25, color='dimgray'))
fig.update_layout(legend=dict(yanchor="auto", y=0.5, xanchor="auto", x=1.3))
fig.update_layout(margin=dict(t=70, b=0, l=0, r=0))
fig.show()
# bar chart
pio.templates.default = "plotly_white"
fig = px.bar(participation2, x='Nom', y='Voix',width=800, height=500, text="Voix", labels={ "Voix": "Nombre de voix","Nom": ""},color = 'Nom',color_discrete_map={
'MACRON':'deeppink',
'LE PEN':'darkblue'})
fig.update_traces(textposition = 'outside',texttemplate='%{text:,}', textfont_size=12)
fig.update_layout(title_text='<b>Nombre de votes/candidat au 2e Tour', title_y=0.97,title_x=0.55,title_font = dict(size=25, color='dimgray'))
fig.update_layout(legend=dict(yanchor="auto", y=0.5, xanchor="auto", x=1.3),legend_title_text='')
fig.update_layout(separators = '.,',margin=dict(t=70, b=0, l=0, r=0))
#fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)')
fig.update_yaxes(visible=False)
fig.show()
rank = data_p8_2tour.groupby(['Département','Code du département','Nom']).sum()
rank = rank.drop(columns =['% Nuls/Ins','Nuls','% Blancs/Vot','Code de la circonscription','Code de la commune','Inscrits','Abstentions','% Abs/Ins','Votants','% Vot/Ins','Blancs','% Blancs/Ins','% Nuls/Vot','Exprimés','% Exp/Ins','% Exp/Vot','% Voix/Ins','% Voix/Exp'])
rank = rank.reset_index(drop=False)
rank.head(2)
| Département | Code du département | Nom | Voix | |
|---|---|---|---|---|
| 0 | Ain | 01 | LE PEN | 111421 |
| 1 | Ain | 01 | MACRON | 173809 |
Dep = rank['Département'].unique()
len(Dep)
107
Vainqueurs = pd.DataFrame()
for i in Dep :
each_dep = rank.loc[(rank['Département'] == i)]
each_win = each_dep[each_dep['Voix'] == each_dep['Voix'].max()]
Vainqueurs = Vainqueurs.append(each_win)
Vainqueurs
| Département | Code du département | Nom | Voix | |
|---|---|---|---|---|
| 1 | Ain | 01 | MACRON | 173809 |
| 2 | Aisne | 02 | LE PEN | 133939 |
| 5 | Allier | 03 | MACRON | 106579 |
| 7 | Alpes-Maritimes | 06 | MACRON | 278407 |
| 9 | Alpes-de-Haute-Provence | 04 | MACRON | 48994 |
| 11 | Ardennes | 08 | MACRON | 64424 |
| 13 | Ardèche | 07 | MACRON | 104599 |
| 15 | Ariège | 09 | MACRON | 47983 |
| 17 | Aube | 10 | MACRON | 75810 |
| 19 | Aude | 11 | MACRON | 100901 |
| 21 | Aveyron | 12 | MACRON | 109340 |
| 23 | Bas-Rhin | 67 | MACRON | 330941 |
| 25 | Bouches-du-Rhône | 13 | MACRON | 519335 |
| 27 | Calvados | 14 | MACRON | 232615 |
| 29 | Cantal | 15 | MACRON | 55411 |
| 31 | Charente | 16 | MACRON | 113700 |
| 33 | Charente-Maritime | 17 | MACRON | 215465 |
| 35 | Cher | 18 | MACRON | 90558 |
| 37 | Corrèze | 19 | MACRON | 88029 |
| 39 | Corse-du-Sud | 2A | MACRON | 31139 |
| 41 | Creuse | 23 | MACRON | 39459 |
| 43 | Côte-d'Or | 21 | MACRON | 159645 |
| 45 | Côtes-d'Armor | 22 | MACRON | 237254 |
| 47 | Deux-Sèvres | 79 | MACRON | 135827 |
| 49 | Dordogne | 24 | MACRON | 135533 |
| 51 | Doubs | 25 | MACRON | 158304 |
| 53 | Drôme | 26 | MACRON | 157992 |
| 55 | Essonne | 91 | MACRON | 382650 |
| 57 | Eure | 27 | MACRON | 158858 |
| 59 | Eure-et-Loir | 28 | MACRON | 122420 |
| 61 | Finistère | 29 | MACRON | 371332 |
| 63 | Français établis hors de France | ZZ | MACRON | 496344 |
| 65 | Gard | 30 | MACRON | 194989 |
| 67 | Gers | 32 | MACRON | 67571 |
| 69 | Gironde | 33 | MACRON | 515940 |
| 71 | Guadeloupe | ZA | MACRON | 100635 |
| 73 | Guyane | ZC | MACRON | 21769 |
| 75 | Haut-Rhin | 68 | MACRON | 203599 |
| 77 | Haute-Corse | 2B | MACRON | 35680 |
| 79 | Haute-Garonne | 31 | MACRON | 437201 |
| 81 | Haute-Loire | 43 | MACRON | 76360 |
| 83 | Haute-Marne | 52 | MACRON | 45192 |
| 85 | Haute-Savoie | 74 | MACRON | 249198 |
| 87 | Haute-Saône | 70 | MACRON | 63541 |
| 89 | Haute-Vienne | 87 | MACRON | 126418 |
| 91 | Hautes-Alpes | 05 | MACRON | 47211 |
| 93 | Hautes-Pyrénées | 65 | MACRON | 79897 |
| 95 | Hauts-de-Seine | 92 | MACRON | 590963 |
| 97 | Hérault | 34 | MACRON | 312419 |
| 99 | Ille-et-Vilaine | 35 | MACRON | 403347 |
| 101 | Indre | 36 | MACRON | 68173 |
| 103 | Indre-et-Loire | 37 | MACRON | 201211 |
| 105 | Isère | 38 | MACRON | 383197 |
| 107 | Jura | 39 | MACRON | 79268 |
| 109 | La Réunion | ZD | MACRON | 212081 |
| 111 | Landes | 40 | MACRON | 146619 |
| 113 | Loir-et-Cher | 41 | MACRON | 100789 |
| 115 | Loire | 42 | MACRON | 218603 |
| 117 | Loire-Atlantique | 44 | MACRON | 525200 |
| 119 | Loiret | 45 | MACRON | 195004 |
| 121 | Lot | 46 | MACRON | 67503 |
| 123 | Lot-et-Garonne | 47 | MACRON | 97418 |
| 125 | Lozère | 48 | MACRON | 26994 |
| 127 | Maine-et-Loire | 49 | MACRON | 288817 |
| 129 | Manche | 50 | MACRON | 177378 |
| 131 | Marne | 51 | MACRON | 144840 |
| 133 | Martinique | ZB | MACRON | 104307 |
| 135 | Mayenne | 53 | MACRON | 112192 |
| 137 | Mayotte | ZM | MACRON | 19140 |
| 139 | Meurthe-et-Moselle | 54 | MACRON | 198750 |
| 141 | Meuse | 55 | MACRON | 48303 |
| 143 | Morbihan | 56 | MACRON | 289594 |
| 145 | Moselle | 57 | MACRON | 282717 |
| 147 | Nièvre | 58 | MACRON | 62722 |
| 149 | Nord | 59 | MACRON | 669806 |
| 151 | Nouvelle-Calédonie | ZN | MACRON | 47902 |
| 153 | Oise | 60 | MACRON | 202509 |
| 155 | Orne | 61 | MACRON | 88484 |
| 157 | Paris | 75 | MACRON | 849257 |
| 158 | Pas-de-Calais | 62 | LE PEN | 382782 |
| 161 | Polynésie française | ZP | MACRON | 52378 |
| 163 | Puy-de-Dôme | 63 | MACRON | 219638 |
| 165 | Pyrénées-Atlantiques | 64 | MACRON | 253617 |
| 167 | Pyrénées-Orientales | 66 | MACRON | 118644 |
| 169 | Rhône | 69 | MACRON | 572015 |
| 171 | Saint-Martin/Saint-Barthélemy | ZX | MACRON | 5282 |
| 173 | Saint-Pierre-et-Miquelon | ZS | MACRON | 1467 |
| 175 | Sarthe | 72 | MACRON | 170153 |
| 177 | Savoie | 73 | MACRON | 135118 |
| 179 | Saône-et-Loire | 71 | MACRON | 166945 |
| 181 | Seine-Maritime | 76 | MACRON | 355441 |
| 183 | Seine-Saint-Denis | 93 | MACRON | 367823 |
| 185 | Seine-et-Marne | 77 | MACRON | 369762 |
| 187 | Somme | 80 | MACRON | 153393 |
| 189 | Tarn | 81 | MACRON | 125591 |
| 191 | Tarn-et-Garonne | 82 | MACRON | 71988 |
| 193 | Territoire de Belfort | 90 | MACRON | 36345 |
| 195 | Val-d'Oise | 95 | MACRON | 342018 |
| 197 | Val-de-Marne | 94 | MACRON | 419145 |
| 199 | Var | 83 | MACRON | 266724 |
| 201 | Vaucluse | 84 | MACRON | 145965 |
| 203 | Vendée | 85 | MACRON | 253914 |
| 205 | Vienne | 86 | MACRON | 143712 |
| 207 | Vosges | 88 | MACRON | 106076 |
| 209 | Wallis et Futuna | ZW | MACRON | 4715 |
| 211 | Yonne | 89 | MACRON | 88939 |
| 213 | Yvelines | 78 | MACRON | 503661 |
#Vainqueurs.to_csv("D:/DA_P8/CSV/Vainqueurs_tour2.csv",index=False,encoding='utf-8-sig')
Count = Vainqueurs['Nom'].value_counts()
Count = Count.reset_index(drop=False)
Count = Count.rename(columns ={'index':'candidat'})
Count = Count.rename(columns ={'Nom':'nbr_département'})
Count
| candidat | nbr_département | |
|---|---|---|
| 0 | MACRON | 105 |
| 1 | LE PEN | 2 |